  const { $Toast } = require('../../utils/iview/base/index');
const Tools = require('../../utils/modules/Tools');
import NetWork from '../../utils/modules/NetWork';

const app = getApp();

var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk;
Page({
  data: {
    province: '', 
    city: '广州市',
    type: 3,
    banner: null,
    student: null,
    shopList: null,
    //门店展示的数据
    shopname: [],
    key: app.globalData._key,
    auth: true,
    // auth: false,
    index: 0,
    vehicle: [
      {
        cp: '粤A•88888',
        name: '捷豹XJ',
        color: '棕色',
        type: '五座车'
      },
      {
        cp: '粤A•88888',
        name: '林肯MKZ',
        color: '白色',
        type: '五座车'
      },
    ],
    isloade: {
      hidden: false
    },
    id:10,
    _baseUrl: app.globalData._baseUrl
  },

  onLoad(o)
  //接口定义了商铺ID这个操作就是取上一页的店铺ID，_that.setData就是数据
   {
    //  console.log(o,o.id,"输出店铺id")
    var _that = this
    _that.setData({
      id: o.id
    })
    _that.setTitle('信息确认');
    
  },

  onShow: function () {
    let _that = this;
    _that.setData({
      isclick: 0
    })

    _that.inshop();
    // _that.isUserInfo();
  },




  /* 拨打电话 */
  telPhone() {
    var that = this;
    wx.makePhoneCall({
      phoneNumber: that.data.shopinfo.phone
    }, res => {

    });
  },

//这部分是“我要洗车”的按钮，这是点击按钮就弹出授权的功能
  purchase() {

    var _that=this;


    var isclick = _that.data.isclick;
    if (isclick) {
      return;
    }

    _that.setData({
      isclick: 1
    })
    setTimeout(() => {
      _that.setData({
        isclick: 0
      })
    }, 2000)


    let auth = _that.data.auth;
    if (auth) {
      _that.isUserInfo();
    }

    setTimeout(() => {
      if (!_that.data.auth) {
        return false;
      }

      wx.navigateTo({
        //参数传输，在这里获取店铺id
        url: '../purchase/purchase',
      })
      wx.setStorageSync("id", this.data.shopinfo.id)
      wx.setStorageSync("store_name", this.data.shopinfo.store_name)
      console.log(this.data.shopinfo.id, "id")
      console.log(this.data.shopinfo.store_name, "店铺ID")
      //这部分是传参和弹出授权没关系
      // wx.navigateTo({
      //   //参数传输，在这里获取店铺id
      //   url: '../purchase/purchase?id=' + this.data.id + '&store_name=' + this.data.store_name,
      // })
      // wx.setStorageSync("id", this.data.shopinfo.id)
      // wx.setStorageSync("store_name", this.data.shopinfo.store_name)
      // console.log(this.data.shopinfo.id, "id")
      // console.log(this.data.shopinfo.store_name, "店铺ID")

    },1000)
      
  
  },
  

  onReady: function (e) {

  },
  formSubmit() {
    console.log("formSubmit");
    $Toast({
      content: '等待门店接单...',
      type: 'loading'
    })
  },



  /* 修改头部标题 */
  setTitle(str) {
    wx.setNavigationBarTitle({
      title: str,
      success: function () {

      }
    })
  },
  onShareAppMessage(str) {
    return {
      title: str,
      path: this.route
    }
  },


  /* 隐藏分享功能 */
  hideShare() {
    wx.hideShareMenu()
  },
  /* 分享功能 */
  showShare() {
    wx.showShareMenu()
  },
  /* 下拉刷新触发功能 */
  onPullDownRefresh() {
    // 下拉刷新事件
    // wx.startPullDownRefresh
    // wx.stopPullDownRefresh
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)

  },

  /* 开启上拉到底部触发功能 */
  startDonwEven() {
    wx.startPullDownRefresh()
  },
  /* 停止上拉到底部触发功能 */
  stopDownEven() {
    wx.stopPullDownRefresh()
  },
  onReachBottom() {
    // 上拉触底事件
    //console.log('触发触底事件')
  },

  onPageScroll(res) {
    // 页面滚动事件
    //console.log(res.scrollTop)
  },


  //获取门店
  inshop() {
    let _that = this;
    //2.还需要获取token
    var token = wx.getStorageSync('token');
    

    //1.在data里面增加完后台字段后需要给它赋值并且在外部也就是这边声明字段
    // var store_id = _that.data;
   
    let net = new NetWork(_that.data._baseUrl)
    net._request({

      //对应后台的API
      url: 'api/v1/member/store_info',
      data: {
        token: token, store_id:_that.data.id, 
      },
      method: 'POST'
    }, '', res => {

      console.log(res.data.data,"res")
      // 对应着代码里面的循环 
    


 
      // _that.setData({
        
      //   //data的个数需要自己测试，如果是少了data控制台输出的参数是会少的，如果多了data的数量会报错因为缺少参数给控制台输出 
      //   shopinfo: res.data.data
      // }) 
      // var multi = decodeURIComponent(_that.data.shopinfo.multi_images)
      // multi = multi.split(',');
      // console.log(multi, "154857")
      // _that.setData({
      // multi: multi,
      // })
      
      var soterin = res.data.data;
      var multi = decodeURIComponent(soterin.multi_images)
      multi = multi.split(',');
      console.log(multi, "154857")
      _that.setData({
        shopinfo: soterin,
        multi: multi
      })





         
    }, res => {
      if (res.data.error_code == 400 && res.data.msg == 'token无效或已过期') {
        app.refreshtoken(token);
        setTimeout(() => {
          _that.inshop();
        }, 1500)
      }
    });
  },

  intoMap: function (e) {
    console.log(e)
    var address = e.currentTarget.dataset.value;
    console.log(address,"dizhi")
    var _this = this;
    //调用地址解析接口
    var qqmapsdk = new QQMapWX({
      key: _this.data.key // 必填
    });
    qqmapsdk.geocoder({
      //获取表单传入地址
      address: address, //地址参数，例：固定地址，address: '北京市海淀区彩和坊路海淀西大街74号'
      success: function (res) {//成功后的回调
        var res = res.result;
        var latitude = res.location.lat;
        var longitude = res.location.lng;


        wx.openLocation({        //所以这里会显示你当前的位置
          latitude: latitude,
          longitude: longitude,
          name: address,
          address: address,
          scale: 28
        })
      },
      fail: function (error) {
        console.error(error);
      },
      complete: function (res) {
        console.log(res);
      }
    })
  },







  isUserInfo() {
    var _that = this;
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          _that.setData({
            auth: false
          })
        } else {
          _that.setData({
            auth: true
          })
        }
      }
    })
  },



  userInfo(e) {
    let _userInfo = e.detail.userInfo;
    let net = new NetWork(app.globalData._baseUrl);

    if (_userInfo) {
      this.setData({
        auth: true
      })
    }

    var {
      signature,
      rawData,
      iv,
      encryptedData
    } = e.detail;

    wx.login({
      success(res) {
        // console.log('小程序登录信息', res)
        if (res.code) {
          net._request({
            url: 'api/v1/member/wxlogin',
            method: 'POST',
            data: {
              code: res.code,
              signature: signature,
              rawData: rawData,
              iv: iv,
              encryptedData: encryptedData
            },
          }, false, sres => {
            if (sres.data.error_code == 0) {

              var _data = sres.data.data;


              // var _data = sres.data.data;
              wx.setStorageSync("user", _data.user)
              wx.setStorageSync("token", _data.access_token)
              // 
              // _that.setData({
              //   token: _data.access_token
              // })

            }
          });
        }
      }
    })


  }







  // userInfo(e) {
  //   let _userInfo = e.detail.userInfo;
  //   let net = new NetWork(app.globalData._baseUrl);

  //   if (_userInfo) {
  //     this.setData({
  //       auth: true
  //     })
  //   }

  //   wx.login({
  //     success(res) {
  //       //console.log('小程序登录信息', res)
  //       if (res.code) {
  //         net._request({
  //           url: 'login',
  //           data: {
  //             code: res.code
  //           },
  //         }, false, sres => {

  //         });
  //       }
  //     }
  //   })




  // }

})